package com.ams.thread.lesson3;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author： 乐哥聊编程(全平台同号)
 */
@Slf4j
public class Example11 {
    public static Object lock = new Object();
    public static void main(String[] args) {
        new Thread(new WaitThread()).start();
        ThreadUtil.sleep(1000);
        new Thread(new NotifyThread()).start();
    }

    static class NotifyThread implements Runnable{

        @Override
        public void run() {
            synchronized (lock){
                log.info("NotifyThread 获得锁");
                lock.notify();
                log.info("通知等待线程 3秒后执行");
                ThreadUtil.sleep(3000);
            }
        }
    }
    static class WaitThread implements Runnable {
        @Override
        public void run() {
            try {
                synchronized (lock){
                    log.info("WaitThread 获得锁");
                    lock.wait();
                    log.info("WaitThread 重新获得锁 继续执行");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
